package com.example.news2.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.example.news2.R;
import com.example.news2.app.GoodsInfoActivity;
import com.example.news2.home.bean.GoodsBean;
import com.example.news2.home.bean.ResultBeanData;
import com.example.news2.home.utils.GlideImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;


import java.util.ArrayList;
import java.util.List;

public class HomeFragmentAdapter extends RecyclerView.Adapter {
    public static final int BANNER = 0;
    public static final int HOT = 1;
    public int currentType = BANNER;

    private ResultBeanData.ResultDTO result;
    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public HomeFragmentAdapter(Context mContext, ResultBeanData.ResultDTO result) {
        this.mContext = mContext;
        this.result = result;
        //通过他来加载布局
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    //加载布局
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        if (viewType == BANNER) {
            return new BannerViewHolder(mLayoutInflater.inflate(R.layout.banner_viewpager, null), mContext, result);
        }else if(viewType ==HOT){
            return new HotViewHolder(mContext, mLayoutInflater.inflate(R.layout.hot_item, null));
        }

        return null;
    }


    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            currentType = BANNER;

        } else {
            currentType = HOT;

        }

        return currentType;
    }

    //加载数据
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        if (getItemViewType(position) == BANNER) {
            BannerViewHolder bannerViewHolder = (BannerViewHolder) viewHolder;
            //设置数据 Banner 的数据
            bannerViewHolder.setData(result.getBannerInfo());
        }else if(getItemViewType(position) ==HOT){
            HotViewHolder hotViewHolder = (HotViewHolder) viewHolder;
            hotViewHolder.setData(result.getHotInfo());
        }
    }
        /**
         * 共有多少个item
         * @return
         */
        @Override
        public int getItemCount () {
            return 2;
        }

        class BannerViewHolder extends RecyclerView.ViewHolder {
            public Banner banner;
            public Context mContext;
            public ResultBeanData.ResultDTO resultBean;

            public BannerViewHolder(View itemView, Context mContext, ResultBeanData.ResultDTO resultBean) {
                super(itemView);
                banner = (Banner) itemView.findViewById(R.id.banner);
                this.mContext = mContext;
                this.resultBean = resultBean;
            }

            public void setData(final List<ResultBeanData.ResultDTO.BannerInfoDTO> banner_info) {
                setBannerData(banner_info);
            }
            private void setBannerData(final List<ResultBeanData.ResultDTO.BannerInfoDTO> banner_info){
                banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
                List<String> imageUris = new ArrayList<>();
                for (int i = 0; i < banner_info.size(); i++) {
                    imageUris.add(banner_info.get(i).getImage());//拿到图片地址
                }
                banner.setImages(imageUris);
                banner.setImageLoader(new GlideImageLoader());
                banner.start();


            }

        }

        private void startGoodsInfoActivity(GoodsBean goodsBean){
            Intent intent=new Intent(mContext, GoodsInfoActivity.class);
            intent.putExtra("goodsBean",goodsBean);
            mContext.startActivity(intent);

        }

    class HotViewHolder extends RecyclerView.ViewHolder{
        private final Context mContext;
        private GridView gv_hot;

        public HotViewHolder(final Context mContext, View itemView) {
            super(itemView);
            this.mContext = mContext;
            gv_hot = (GridView) itemView.findViewById(R.id.gv_hot);

        }


        public void setData(final List<ResultBeanData.ResultDTO.HotInfoDTO> hot_info) {
            //1.有数据
            //2.设置GridView的适配器
            HotGridViewAdapter  adapter = new HotGridViewAdapter(mContext,hot_info);
            gv_hot.setAdapter(adapter);
            gv_hot.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    ResultBeanData.ResultDTO.HotInfoDTO hotInfoDTO =  hot_info.get(position);
                    //商品信息类
                    GoodsBean goodsBean = new GoodsBean();
                    goodsBean.setCover_price(hotInfoDTO.getCoverPrice());
                    goodsBean.setFigure(hotInfoDTO.getFigure());
                    goodsBean.setName(hotInfoDTO.getName());
                    goodsBean.setProduct_id(hotInfoDTO.getProductId());
                    startGoodsInfoActivity(goodsBean);
                }
            });
        }
    }


}
